package springboot.j2024_11_3;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/he")
@RestController
public class Hello {
////    @RequestMapping(value = "/hello" , method = RequestMethod.GET)
////    public String hello(){
////        return "你好";
////    }
////
////    @RequestMapping("/r1")
////    public String r1(Integer age){
//////        HttpServletRequest request ;
//////        String name = request.getParameter("name");
////        return "响应:age="+age;
////    }
////    @RequestMapping("/r2")
////    public String r2(String name , Integer age){
////        return "name:"+name +" age:"+age;
////    }
////
////    @RequestMapping("/r3")
////    public String r3(UserInfo user){
////        return  user.toString() ;
////    }
////
////    @RequestMapping("/r4")
////    public String r4(@RequestParam(value = "name" ,required = false ) String username   ){
////        return  username;
////    }
////
////    @RequestMapping("/r5")
////    public String r5(String[] arr){
////      return Arrays.toString(arr)+"len:"+arr.length;
////    }
////    @RequestMapping("/r6")
////    public String r6(@RequestParam(value = "list" ,required = false) List<Integer> list){
////        return list==null?"list为空": list.toString();
//    }

    @RequestMapping(value = "/hello" ,method = RequestMethod.GET)
    public String hello(@RequestParam(value = "list" , required = false) List<Integer> list){

        return list==null?"list为空": list.toString();
    }

    @RequestMapping("/r1")
    public String r1(UserInfo user){
        return user.toString();
    }

    @RequestMapping("/r2")
    public  String r2(@RequestBody UserInfo userInfo){
          return userInfo.toString();
    }
    @RequestMapping("/r3/{name}/{id}")
    public String r3(@PathVariable(value = "name") String username , @PathVariable("id") Integer userid){
         return "username:"+ username + "userid:"+userid;
    }

    @RequestMapping("/r4")
    public String r4 (@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();   //文件名称
        //将文件 上传到xx地址 ,命名为 filename名字;
        file.transferTo(new File("xx地址/"+fileName));
        return "上传的文件:"+ file.getOriginalFilename(); // 获取文件名称 ;
    }

}
